<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="课程列表" name="first">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button @click="handleChangeType()" round :type="queryParams.type ? '' : 'primary'" >全部</el-button>
            <el-button @click="handleChangeType(item)" round :type="queryParams.type==item.id ? 'primary' : ''" v-for="item in typeList" :key="item.id">{{ item.name }}</el-button>
          </el-col>
          <el-button type="primary" @click="handleAdd" style="float: right; margin-right: 6px;">创建课程</el-button>
        </el-row>
        <div v-show="total == 0" style="padding: 80px 0;"><el-empty description="暂无课程"></el-empty></div>
        <el-row justify="space-between" :gutter="40">
          <el-col class="panel-col" v-for="(item, index) in list" :key="index" :xs="12" :sm="12" :lg="6">
            <el-card :body-style="{ padding: '0px' }">
              <div @click="handleDetail(item)">
                <img :src="item.cover" class="image">
                <div style="padding: 14px;">
                  <span>{{ item.name }}</span>
                  <div class="bottom">
                    <span class="teacher">授课教师：{{ item.username }}</span>
                    <el-button type="text" icon="el-icon-user" class="button">{{ item.num }}</el-button>
                  </div>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </el-tab-pane>
    </el-tabs>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="课程类别" prop="type">
          <el-select v-model="form.type" placeholder="请选择课程类别" size="small">
          <el-option
            v-for="dict in typeList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="是否显示" prop="visible">
          <el-select v-model="form.visible" placeholder="是否显示" size="small">
            <el-option
              v-for="dict in dict.type.sys_bool_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="课程封面" prop="cover">
          <ImageUpload :limit="1" :fileSize="2" @input="handleUploadSuccess"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCourseType } from "@/api/edu/courseType";
import { listCourse, getCourse, addCourse, updateCourse, delCourse } from "@/api/edu/course";

export default {
  name: "CourseType",
  dicts: ['sys_bool_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      activeName: "first",
      typeList: [],
      fileType: ['png', 'jpg', 'jpeg'],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 20,
        type: undefined
      },
      // 表单参数
      form: {
        id: undefined,
        name: undefined,
        type: undefined,
        visible: '0',
        startTime: undefined,
        endTime: undefined,
        cover: undefined
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getCourseTypeList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCourseTypeList(){
      listCourseType({status: 1, page: 1, limit: 100}).then(response => {
        this.typeList = response.rows;
      });
    },
     handleDetail(item){
      this.$router.push({ path: '/drill/courseDetail?courseId='+item.id });
    },
    handleChangeType(item){
      if(item){
        this.queryParams.type = item.id;
      } else {
        this.queryParams.type = undefined;
      }
      this.handleQuery();
    },
    handleClick(){},
    handleUploadSuccess(data){
      this.form.cover = data;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        type: undefined,
        visible: '0',
        startTime: undefined,
        endTime: undefined,
        cover: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getCourse(row.id).then(response => {
        this.form = response.data;
        this.form.status = this.form.status + '';
        this.open = true;
        this.title = "修改课程";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCourse(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCourse(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    }
    
  }
};
</script>
<style scoped lang="scss">
  .mb8{
    padding: 10px 0;
  }
  .panel-col{
    padding-bottom: 20px;
  }
  .teacher {
    font-size: 13px;
    color: #999;
  }
  
  .bottom {
    margin-top: 13px;
    line-height: 12px;
    display: flex;
    justify-content: space-between;
  }

  .button {
    padding: 0;
    float: right;
  }

  .image {
    width: 100%;
    display: block;
  }
</style>